package cc.rengu.igas.channel.wechat.core.service.trans;

import cc.rengu.igas.channel.wechat.common.constant.WechatConfigConstant;
import cc.rengu.igas.channel.wechat.common.constant.WechatServiceConstant;
import cc.rengu.igas.channel.wechat.common.constant.WechatTreeNodeConstant;
import cc.rengu.igas.channel.wechat.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.wechat.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.wechat.core.service.base.MchntService;
import cc.rengu.igas.channel.wechat.facade.request.ActivityRateDiscountRegistRequest;
import cc.rengu.igas.channel.wechat.facade.response.ActivityRateDiscountRegistResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelCfg;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSONObject;

/**
 * @version 1.0.0
 * @date 2021/3/8 11:36
 */
public class ActivityRateDiscountRegistService extends MchntService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        ActivityRateDiscountRegistRequest activityRateDiscountRegistRequest = new ActivityRateDiscountRegistRequest();
        ConvertUtil.convertOutput(activityRateDiscountRegistRequest);
        rglog.debug("beforeBizProcess ---- activityRateDiscountRegistRequest = <{}>", JSONObject.toJSONString(activityRateDiscountRegistRequest));
        return activityRateDiscountRegistRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;

        ActivityRateDiscountRegistResponse activityRateDiscountRegistResponse = new ActivityRateDiscountRegistResponse();
        ActivityRateDiscountRegistRequest activityRateDiscountRegistRequest = (ActivityRateDiscountRegistRequest) request;

        DstChannelCfg dstChannelCfg = (DstChannelCfg) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_CFG, DstChannelCfg.class);
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(activityRateDiscountRegistRequest.getHeader().getInstId(),
                WechatConfigConstant.WECHAT_CONF_NODE, WechatConfigConstant.WECHAT_ACTIVITY_RATE_DISCOUNT_REGIST_URL);
        if (sysParam == null) {
            rglog.error("获取微信费率优惠活动报名url失败，请配置");
            throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
        }
        ConvertUtil.convertInput(activityRateDiscountRegistRequest);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, activityRateDiscountRegistRequest.getHeader().getTxnNum());
        //设置从业机构号和渠道商户号
        xmlTreeUtil.setXmlTreeStringValue("acquirngBankId", dstChannelCfg.getProviderMchntNo());
        xmlTreeUtil.setXmlTreeStringValue("wechatChannelId", dstChannelCfg.getChannelNo());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.DEFINE_HTTP_METHOD_NODE, "POST");
        /*encSensitiveInfo(activityRateDiscountRegistRequest,dstChannelCfg);*/
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE, sysParam.getParamValue());
        //走  V3客户端  WechatAuthClientService
        iReturnCode = schedule.callService(WechatServiceConstant.WECHAT_V3_CLIENT_SERVICE, 0);

        if (xmlTreeUtil.isExist(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE)) {
            xmlTreeUtil.deleteXmlTreeNode(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE);
        }

        if (0 != iReturnCode && 98 != iReturnCode) {
            rglog.error("调用微信服<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        } else if (98 == iReturnCode) {
            rglog.error("调用微信服<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.TIME_OUT_ERROR.getRespCode(), RespCodeEnum.TIME_OUT_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(activityRateDiscountRegistResponse);
        String returnCode = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.RESP_CODE);
        String returnMsg = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.RESP_DESC);
        if (!RespCodeEnum.TRANS_SUCCESS.getRespCode().equals(returnCode)) {
            activityRateDiscountRegistResponse.setRespCode(returnCode);
            activityRateDiscountRegistResponse.setRespDesc(returnMsg);
            rglog.error("调用第三方服务失败,respCode=[{}],respMsg=[{}]", activityRateDiscountRegistResponse.getRespCode(), activityRateDiscountRegistResponse.getRespDesc());
        } else {
            activityRateDiscountRegistResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            activityRateDiscountRegistResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        chnlBizResponse.setResult(activityRateDiscountRegistResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        ActivityRateDiscountRegistResponse activityRateDiscountRegistResponse = (ActivityRateDiscountRegistResponse) response.getResult();
        ConvertUtil.convertInput(activityRateDiscountRegistResponse);
    }
}
